package auction
import grails.plugin.spock.UnitSpec
import used.UsedCar

class FilterCarsSpec extends UnitSpec{
	/*
Long websiteId
	BigDecimal price
	String title
	Long mileage
	String transmission
	String engine
	String fuel
	Integer year
New approach to mocking domain classes in Grails unit tests
	*/
	def "Cars that are in the db and have been newly parsed are updated"(){
		given: "That cars have been parsed from a website"
		def car = new ParsedCar(id: 123,  price: 55, title: 'parsed pinto')
		def parsedCars = [car]

		and: "The database has cars in it"
		def pinto = new UsedCar(id: 1, websiteId: 123, price: 33, title: 'old pinto')
		mockDomain UsedCar, [pinto]

		when: "Parsed cars updates the records of the cars in the db"
		new CarInfoService().filterListOfCars(parsedCars)

		then: "the records in the db are updated"
		def updatedPinto = UsedCar.get(pinto.id)
		updatedPinto.price == 55
	}

	def "Cars that aren't in the db are added to the db"(){
		given: "That cars have been parsed"
		def car = new ParsedCar(id: 123,  price: 55, title: 'parsed pinto')
		def parsedCars = [car]

		and: "The database has no cars in it"
		mockDomain UsedCar

		when: "filtered"
		new CarInfoService().filterListOfCars(parsedCars)

		then: "new cars are in the db"
		1 == UsedCar.count()
	}

	def "Cars that have been parsed are in the db and aren't in the db get updated or added"(){
		given: "That cars have been parsed from a website"
		def updatedPrice = 55
		def car = new ParsedCar(id: 123,  price: updatedPrice, title: 'parsed pinto')
		def carNotInDB = new ParsedCar(id: 333,  price: 44, title: 'parsed porche')
		def parsedCars = [car, carNotInDB]

		and: "The database has cars in it"
		def pinto = new UsedCar(id: 1, websiteId: 123, price: 33, title: 'old pinto')
		mockDomain UsedCar, [pinto]

		when: "Parsed cars updates the records of the cars in the db"
		new CarInfoService().filterListOfCars(parsedCars)

		then: "the records in the db are updated"
		def updatedPinto = UsedCar.get(pinto.id)
		updatedPinto.price == updatedPrice

		and: "There are 2 cars in the db"
		2 == UsedCar.count()
	}
}
